package com.neo.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.UUID;

public class MD5Util {
    /***
     * MD5加密 生成32位md5码
     *
     * @param 待加密字符串
     * @return 返回32位md5码
     * @throws UnsupportedEncodingException
     */
    public static String encode(String inStr) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray = inStr.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuilder hexValue = new StringBuilder();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取uuid进行MD5加密的字符串
     *
     * @return
     */
    public static String getMD5UUID() {
        UUID uuid = UUID.randomUUID();
        return MD5Util.encode(uuid.toString());
    }

}
